package com.moto.service.impl;

import com.moto.Vo.LogVo;
import com.moto.Vo.UserLogVo;
import com.moto.constant.MessageConstant;
import com.moto.entity.Emp;
import com.moto.entity.User;
import com.moto.entityDto.LogDto;
import com.moto.entityDto.UpdateUser;
import com.moto.entityDto.UserLoginDto;
import com.moto.entityDto.UserRegisterDto;
import com.moto.exception.AccountNotFoundException;
import com.moto.exception.PasswordErrorException;
import com.moto.mapper.EmpMapper;
import com.moto.mapper.UserMapper;
import com.moto.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Random;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author afei
 * @since 2023-08-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(UserLoginDto userLoginDto) {
        System.out.println("phone:::"+userLoginDto.getPhone());
        String phone = userLoginDto.getPhone();
        String password = userLoginDto.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByPhone(phone);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException("账号不存在");
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(password);
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }


        //3、返回实体对象
        return user;
    }

    @Override
    public int findPhoneIsNull(UserRegisterDto userRegisterDto) {
        String phone = userRegisterDto.getPhone();
        return userMapper.findPhoneIsNull(phone);
    }

    @Override
    public void registerUser(UserRegisterDto userRegisterDto) {
        String phone = userRegisterDto.getPhone();
        String password = userRegisterDto.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //获取四位随即英文
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < 4; i++) {
            char randomChar = ALPHABET.charAt(random.nextInt(ALPHABET.length()));
            builder.append(randomChar);
        }


        User user = new User();
        user.setPassword(password);
        user.setPhone(phone);
        user.setPhoto("https://img1.baidu.com/it/u=3709586903,1286591012&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        user.setAge(0);
        user.setAddress("未知");
        user.setNickname(builder.toString());
        user.setSex("男");
        user.setUsername("---");

        userMapper.registerUser(user);
    }

    @Override
    public void updateUser(UpdateUser updateUser) {
        String password = updateUser.getPassword();
        if (password!=null) {
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            updateUser.setPassword(password);
        }
        userMapper.updateUser(updateUser);
    }


}
